<template>
  <el-dialog
    class="assign-role"
    title="分配角色"
    :visible="showRoleDialog"
    width="40%"
    @close="close"
    @open="showMyRole"
  >
    <!-- 这里准备复选框   v-loading="loading"-->
    <el-checkbox-group v-model="checkList" v-loading="loading">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="assignRole">确定</el-button>
      <el-button size="small" @click="close">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/settings'
import { getUserDetail } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      list: [],
      checkList: [],
      loading: false
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const res = await getRoleList(1, 10000)
      //   console.log(res, 'all')
      this.list = res.rows
    },
    close() {
      this.$emit('update:showRoleDialog', false)
    },
    async showMyRole() {
      this.loading = true
      // 获取员工基本信息 中查看角色 roleIds
      const res = await getUserDetail(this.userId)
      this.checkList = res.roleIds
      this.loading = false

    //   console.log(res, 'my')
    },
    // 给用户分配角色
    async assignRole() {
      await assignRoles({ id: this.userId, roleIds: this.checkList })
      this.$emit('update:showRoleDialog', false)
      this.$message.success('分配角色成功')
      // 不需要重新 渲染 因为点击的时候就渲染了 ,更新之后会关闭弹窗  再点击也会重新渲染
    }
  }
}
</script>
